<?php

namespace app\common\model;

use app\common\model\TimeModel;
use think\Exception;

class DimCategory extends TimeModel
{

    protected $name = "dim_category";

    protected $deleteTime = false;

    const TABLE_NAME = 'dim_category';

    // 是否删除
    const IS_DELETE_NO = 0;
    const IS_DELETE_YES = 1;
    public static $is_delete_map = array(
        self::IS_DELETE_NO => '否',
        self::IS_DELETE_YES => '是'
    );

    const CATE_TYPE_TASK = 1;
    const CATE_TYPE_NEWS = 2;
    public static $cate_type_map = array(
        self::CATE_TYPE_TASK => '任务分类',
        self::CATE_TYPE_NEWS => '文章分类'
    );

    /**
     * 获取分类树
     *
     * @param array $conditions
     * @return array
     */
    public function getTree($conditions=array())
    {

        // 分类列表
        $cate_tree = [];
        try{
            $cate_tree_list = model(DimCategory::TABLE_NAME)->getList($conditions);
            $cate_tree = buildPidMenu(0, $cate_tree_list);
        }catch (Exception $e){}

        return $cate_tree;
    }

    /**
     * 获取分类列表
     *
     * @param $condition
     * @param string $field
     * @param string $order
     * @return mixed
     * @throws Exception
     */
    public function getList($condition, $field='id, title, sort', $order='id desc')
    {

        $new_conditions = $condition;
        $new_conditions[] = array('is_del', '=', self::IS_DELETE_NO);
        try{
            return model(self::TABLE_NAME)->getList($new_conditions, $field, $order);
        }catch (\Exception $e){
            self::writeLog([
                'sql' => lastSql(),
                'msg' => $e->getMessage()
            ]);
            throw new Exception('查询失败');
        }
    }
}